#!/usr/bin/env sh

install_me()
{
	whomedir="$1";
	chmod 700 "$whomedir":
	mkdir "$whomedir";
	touch "$whomedir/socket_name";
	mkdir "$whomedir/sockets";
}

vecho()
{
	if [ -n "$verbose" ]
	then
		echo $@;
	fi
}

cleanup="";
ncurse="";
optdir=~/.vlc_extra;
verbose="";
while getopts co:nv opt
do
	case "$opt" in
		'c') cleanup="YES";;
		'n') ncurse="YES";;
		'o') optdir="$OPTARG";;
		'v') verbose="YES";;
		'?') exit 1;;
	esac
done
# if we don't exist lettuce install ourself
if [ ! -d "$optdir" ]
then
	install_me "$optdir";
fi

[ -n "$cleanup" ] && vecho "Yeah!";
base="$(basename $0)";
. vlc.getsock.sh
if [ -n "$cleanup" ]
then
	vecho "cleaning up old sockets....";
	# exit program if currently running
	vlc.do -c quit;
	# remove the socket file (not deleted before if program not running)
	rm $current_sock;
	# empty the current socket file...
	:>"$optdir/socket_name";
	#reset the current socket 
	. vlc.getsock.sh
fi

if [ -n "$current_sock" ]
then
	vecho checking to see if program is already running...;
	if [ isrunning ]
	then
		Xdialog --icon ~/bin/icons/warning.xpm --infobox "VLC already running...." 0 0 2000;
		return 1;
	else
		Xdialog --icon ~/bin/icons/warning.xpm --infobox "Last VLC proccess failed to clean up...." 0 0 2000;
	fi
fi
TMPFILE=$(mktemp $optdir/sockets/XXXXXX);
#we can't operate if the file exists....
rm "$TMPFILE";
vecho $TMPFILE;
echo "current_sock=\"$TMPFILE\"">$optdir/socket_name;
# -L == loop; -d == deamon;
vlc_opts="--rc-unix "$TMPFILE" --rc-fake-tty -L --no-media-library -d --volume 1024";
if [ -n "$ncurse" ]
then
	vlc --intf rc --extraintf ncurses $vlc_opts;
else
	vlc --intf rc $vlc_opts;
fi
return 0;
